package com.du.springmvc;

import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author : DuJiabao
 * @Project : spring-mvc
 * @Time : 2021/4/7 16:47
 * @Desc :
 */
public class MyLocaleResolver implements LocaleResolver {
  @Override
  public Locale resolveLocale(HttpServletRequest request) {
    String localeStr = request.getParameter("locale");
    if (localeStr == null || "".equals(localeStr))
      return request.getLocale();
    String language = localeStr.split("_")[0];
    String country = localeStr.split("_")[1];
    return new Locale(language, country);
  }

  @Override
  public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    throw new UnsupportedOperationException(
            "Cannot change HTTP accept header - use a different locale resolution strategy");
  }
}
